<?php

require_once(LIB . "/board/Post.php");
require_once(LIB . "/database/CommonRepository.php");

class PostRepository extends CommonRepository
{

	function PostRepository()
	{
		parent::CommonRepository();
		$this->connect();
	}
	
	function load($idx)
	{
		$query = "select * from posts where idx='" . $idx . "';";
		$this->query($query);
		if ($this->fetch()) {
			$post = new Post();
			$post->idx = $this->obj->idx;
			$post->subject = $this->obj->subject;
			$post->contents = $this->obj->contents;
			$post->regdate = $this->obj->regdate;
		}
		else {
			$post = null;
		}
		
		return ($post);
	}
	
	function insert($post)
	{
		$query = "insert into posts (subject, contents) values(";
		$query .= "'" . $post->subject . "'";
		$query .= ",'" . $post->contents . "');";
		
		$this->query($query);
		return ($this->insertId);
	}
	
	function loadList()
	{
		$query = "select * from posts;";
		$this->query($query);
		$count = $this->numRows;
		for ($i = 0 ; $i < $count ; $i++) {
			$this->fetch();
			
			$post = new Post();
			$post->idx = $this->obj->idx;
			$post->subject = $this->obj->subject;
			$post->contents = $this->obj->contents;
			$post->regdate = $this->obj->regdate;
			$posts[$i] = $post;
		}
		
		return ($posts);
	}
	
	function delete($idx)
	{
		if (empty($idx)) {
			$idx = 0;
		}
		$query = "delete from posts where idx=" . $idx;
		$this->query($query);
		return ($this->numRows);
	}
	
	function update($idx, $post)
	{
		if (empty($idx)) {
			$idx = 0;
		}
		$query = "update posts set";
		$query .= " subject='" . $post->subject . "'";
		$query .= " , contents='" . $post->contents . "'";
		$query .= " where idx=" . $idx . "";
		$this->query($query);
		return ($this->numRows);
	}

}
?>
